<?php
if(!isset($_SESSION['user'])){
die();
}

require("public/exts/xcrud/xcrud.php");
		if(isset($_GET['pid'])){
			$_GET['pid']=intval($_GET['pid']);
			$db=new db;
			$db->connect();
			$db->query("select id,name,alias,upload_user from blog_post where id=".intval($_GET['pid']));
			if($db->num_rows()>0){
				$r=$db->fetchOne();
				if($_SESSION['user']['level']!=1&&$_SESSION['user']['level']!=6){
					//Neu ko phai la admin va mod thi phai kiem tra xem user co phai la nguoi dang bai hay khong
					if($r['upload_user']!=$_SESSION['user']['username']){
						die('You don\'n have permission to do this task ');
					}
				}
			}else{
					die('Nội Dung Không Tồn Tại');
			}
		$db->close();	
		}

		$xcrud = Xcrud::get_instance();
		
	//User level ==6, Blog moderator-----------
		
        $xcrud->table('blog_post');
		if($_SESSION['user']['level']!=1&&$_SESSION['user']['level']!=6&&$_SESSION['user']['level']!=2){
		$xcrud->where('upload_user', $_SESSION['user']['username']);
		}
		if(isset($_GET['pid'])){
			$xcrud->where('id', intval($_GET['pid']));
		}
		
		if(isset($_GET['ext'])&& $_GET['ext']=='duyet'){
				if($_SESSION['user']['level']!=1&&$_SESSION['user']['level']!=6&&$_SESSION['user']['level']!=2){
					die("Not find data !!");
				}
			$xcrud->where('status', 0);
		}
		if($_SESSION['user']['level']==1 || $_SESSION['user']['level']==6){
		$xcrud->columns('id,name,category,subcategory,tag,upload_time,upload_user,status,pick_home,pick_category,pick_subcategory');
		}else{
		$xcrud->columns('id,name,category,subcategory,tag,upload_time,upload_user,status');
		}
		//Dependence dropdown
		$xcrud->relation('category','blog','id','name','blog.type = "blog" and blog.parent is NULL');
		$xcrud->relation('subcategory','blog','id','name','','','',' ','','parent','category');
		//$xcrud->relation('field','table','id','name','','','',' ','','parent','category');
		//Dependence dropdown
		if($_SESSION['user']['level']==1 || $_SESSION['user']['level']==6){
		$xcrud->fields('name,description,category,subcategory,tag,content',false,'Thông Tin');
		$xcrud->fields('pick_home,pick_category,pick_subcategory',false,'Pick');
		}else{
		$xcrud->fields(array('name','description','category','subcategory','tag','content'));
		}
		
		
		if($_SESSION['user']['level']==1 || $_SESSION['user']['level']==6 ){
			//Action duyet post
			$xcrud->create_action('pass_blog_post', 'blog_pass_post'); //duyet truyen 
			$xcrud->column_pattern('status','<a class="xcrud-button xcrud-action" href="#"  data-task="action" data-action="pass_blog_post" data-primary="{id}"><i class="glyphicon glyphicon-ok state10-{status}"></i><i class="glyphicon glyphicon-remove state10-{status}"></i></a>'); 
		}else{
			//Only display status, not action include
			$xcrud->column_pattern('status','<i class="glyphicon glyphicon-ok state10-{status}"></i><i class="glyphicon glyphicon-remove state10-{status}"></i>'); 
		}
		
		
		if($_SESSION['user']['level']==1||$_SESSION['user']['level']==2){
			$xcrud->create_action('blog_pick_post_home', 'blog_pick_post_home'); //duyet truyen 
			$xcrud->column_pattern('pick_home','<a class="xcrud-button xcrud-action" href="#"  data-task="action" data-action="blog_pick_post_home" data-primary="{id}"><i class="glyphicon glyphicon-ok state10-{pick_home}"></i><i class="glyphicon glyphicon-remove state10-{pick_home}"></i></a>'); 
			
			$xcrud->create_action('blog_pick_post_category', 'blog_pick_post_category'); //duyet truyen 
			$xcrud->column_pattern('pick_category','<a class="xcrud-button xcrud-action" href="#"  data-task="action" data-action="blog_pick_post_category" data-primary="{id}"><i class="glyphicon glyphicon-ok state10-{pick_category}"></i><i class="glyphicon glyphicon-remove state10-{pick_category}"></i></a>'); 
			
			$xcrud->create_action('blog_pick_post_subcategory', 'blog_pick_post_subcategory'); //duyet truyen 
			$xcrud->column_pattern('pick_subcategory','<a class="xcrud-button xcrud-action" href="#"  data-task="action" data-action="blog_pick_post_subcategory" data-primary="{id}"><i class="glyphicon glyphicon-ok state10-{pick_subcategory}"></i><i class="glyphicon glyphicon-remove state10-{pick_subcategory}"></i></a>');
		}
	
		
		//Apply select2 example
		$xcrud->set_attr('tag',array('id'=>'tag_select2'));
		//End select2
		
		
		$xcrud->label(array('name' => 'Tiêu Đề','description'=>'Mô Tả',"url"=>"Link Youtube","category"=>"Danh Mục","content"=>"Nội Dung","image"=>"Hình Ảnh"));
			
		$xcrud->unset_csv();
		$xcrud->unset_print();
		$xcrud->unset_title();
		
		//Unset Remove nếu không phải là amdin và moderator
		if($_SESSION['user']['level']!=1&&$_SESSION['user']['level']!=6){
			$xcrud->unset_remove();
		}
		//
		
		$xcrud->no_quotes("alias");
		
		//Handle data function--------------------------
		$xcrud->before_insert("blog_post_before_insert","blog.php");
		$xcrud->after_insert("blog_post_after_insert","blog.php");
		$xcrud->before_update("blog_post_before_update","blog.php");
		//End handle data function ------------------------
		
		$xcrud->pass_var('upload_user',$_SESSION['user']['username'],'create');
		$xcrud->pass_var('upload_time',date('Y-m-d'),'create');
		
		$xcrud->pass_var('update_user',$_SESSION['user']['username'],'edit');
		$xcrud->pass_var('update_time',date('Y-m-d h:i:s'),'edit');
		
		
		$xcrud->set_lang('save_return','Lưu Lại & Trở Về');
		$xcrud->set_lang('return','Trở Về');
		$xcrud->set_lang('add','Thêm Bài Viết Mới');
		$xcrud->set_lang('save_edit','Lưu Lại & Tiếp Tục Cập Nhật Thông Tin');
		$xcrud->set_lang('save_new','Lưu Lại & Và Thêm Bài Viết Khác');
	?>
      	 <ul class="breadcrumb">
          	<li><a href="<?php echo HOME; ?>"><i class="fa fa-home"></i> Home</a></li>
             <li><a href="<?php echo HOME;?>index.php?module=manage&act=home">Quản Lý</a></li>
              <li><a href="<?php echo HOME;?>index.php?module=manage&act=blog&opt=post">Blog</a></li>
              <?php if(isset($_GET['pid'])){?>
			   <li><a href="<?php echo HOME;?>index.php?module=manage&act=blog&opt=post&pid=<?php echo $_GET['pid']; ?>"><?php echo $r['name']; ?></a></li>
			 <?php }?>
          </ul>
    
    <?php
	//echo $xcrud->render('edit', 1); 
	echo $xcrud->render();


?>
      
           
         <script type="text/javascript">
jQuery(document).on("xcrudbeforerequest", function(event, container) {
    if (container) {
        jQuery(container).find("#tag_select2").select2("destroy");
    } else {
        jQuery(".xcrud").find("#tag_select2").select2("destroy");
    }
});
jQuery(document).on("ready xcrudafterrequest", function(event, container) {
    if (container) {
		$("#tag_select2").removeClass("form-control");

        jQuery(container).find("#tag_select2").select2({
				 multiple: true,
				 width: '100%',
				 	 maximumSelectionSize: 1,
       				formatSelectionTooBig: function (limit) {
            		return 'Chỉ Được Tạo 1 tag cho bài viết';
       				 },
       				formatSelection: function(object){
       					object.text = cutHintString(object);
       					return object.text;
       				},
				 
        		minimumInputLength: 8,
				 initSelection : function (element, callback) {
					var data = [];
					$(element.val().split(",")).each(function () {
						data.push({id: this, text: this});
					});
					callback(data);
				},
        			ajax: { 
        			url: "index.php?module=manage&act=blog&opt=ajax&feed_tag_blog", 
        			dataType: 'json', 
					 quietMillis: 1000,
        					data: function (term, page){ 
        						return {q: term }; 
        						}, 
        						results: function (data, page) {
        							
									
        							return { results: data }; 
									
        						}
        			}
			});
    } else {
        jQuery(".xcrud").find("#tag_select2").select2({
			  multiple: true,
				 width: '100%',
				 separator: ',',
				  maximumSelectionSize: 1,
       				formatSelectionTooBig: function (limit) {
            		return 'Chỉ Được Tạo 1 tag cho bài viết';
       				 },
       				formatSelection: function(object){
       					object.text = cutHintString(object);
       					return object.text;
       				},
        		minimumInputLength: 8,
        			ajax: { 
        			url: "index.php?module=manage&act=manga&opt=ajax&feed_tag_blog", 
        			dataType: 'json', 
					 quietMillis: 1000,
        					data: function (term, page){ 
        						return {q: term }; 
        						}, 
        						results: function (data, page) {
        							
									//data = cutHintString(data);
        							return { results: data }; 
									
        						}
        			}
        			
			
			});
    }
});



function cutHintString(data){
	var cut_string = "[ Thêm Từ Khóa Này ]";
	if(data){
		if(data.text.indexOf(cut_string)){
			data.text = data.text.replace(cut_string, " ");
		}
		return data.text;
	}
}

</script>
        <style>
    .glyphicon-ok.state01-1,.glyphicon-remove.state01-0{
    display:none;
	}
	.state01-0{ color: #0C6}
	.state01-1{ color: #F00}
	.glyphicon-ok.state10-0,.glyphicon-remove.state10-1{
    display:none;
	}
	.state10-1{ color:#0C6 }
	.state10-0{ color: #F00}
	</style>